package com.lfd.rocketmq.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lfd.rocketmq.pojo.NewOrder;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @项目名： spring-emq
 * @包名： com.lfd.rocketmq.listener
 * @文件名: ObjMsgListener
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/10/1 20:31
 * @描述：创建一个对象消息的监听
 *  * 1.类上添加注解@Component和@RocketMQMessageListener
 *  * 2.实现RocketMQListener接口，注意泛型的使用
 */
@Component
@Slf4j
@RocketMQMessageListener(topic = "powernode-obj",
consumerGroup = "powernode-obj-group")
public class ObjMsgListener implements RocketMQListener<NewOrder> {
    @Autowired
    private ObjectMapper objectMapper;
    /**
     * 消费消息的方法
     *
     * @param newOrder
     */
    @Override
    public void onMessage(NewOrder newOrder) {
        try {
            log.info("onMessage newOrder:{}", objectMapper.writeValueAsString(newOrder));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
